<?php namespace Common\Model;
use Hdphp\Model\Model;
use Common\Model\Cate;
use Common\Model\TypeAttr;
use Common\Model\Goods;
use Common\Model\GoodsAttr;
use Common\Model\Detail;
class Goods extends Model{
	protected $table = 'Goods';
	
	//设置自动验证用户提交过来的参数
	protected $validate = array(
		//array(字段名,验证方法,错误信息,验证条件,验证时间)
		array('gname','required','标签名称不能为空',3,3),
		
	);
		//设置自动完成
	protected $auto=[
        //array(字段名,处理方法,方法类型,验证条件,处理时间) 
		array('time','time','function',3,1)
    ];
	
	/*制作  1=小图small  2=中图medium  3=大图big */
	private function makeimg($imgtype,$imgArr){
		$srcs = '';
		foreach ($imgArr as $path) {
			
			$tmpPath = substr_replace($path,"_{$imgtype}.",strripos($path,'.'),1);//组建缩略图地址
			if($imgtype=='small'){
				Image::thumb($path,$tmpPath,100,100,5);
			}elseif ($imgtype=='medium') {
				Image::thumb($path,$tmpPath,468,616,5);
			}else{
				Image::thumb($path,$tmpPath,600,800,5);
			}
			
			$srcs .=  $tmpPath.',';
		}
			
		
		return $srcs;
	}
	public function myedit(){
		
		if($this->create()){
			//p(Q('post.'));exit;
			$gid = Q('post.gid',0,'intval');
			$this->save();//添加到商品表 返回自增id
			
			//添加商品详情
			$goodsDetail = new Detail;
					
			$goodsDetail->where("gid","=",$gid)
			->update(array(
					'intro'=>Q('post.intro'),
					'service'=>Q('post.service')
						
				));
			
			return true;
		}
		
		return false;
		
	}
	
	public function myadd(){
		
		if($this->create()){
//			p(Q('post.'));exit;
			$gid = $this->add();//添加到商品表 返回自增id
			
			//添加商品详情
			$goodsDetail = new Detail;
			$imgs = Q('post.detail_img.');//获取图册
			//p($imgs);exit;
			$detailData=array(
				'small'=>$this->makeimg('small',$imgs),
				'medium'=>$this->makeimg('medium',$imgs),
				'big'=>$this->makeimg('big',$imgs),
				'intro'=>Q('post.intro'),
				'service'=>Q('post.service'),
				'gid'=>$gid
				);
			$goodsDetail->add($detailData);
			
			//添加商品属性
			$goodsAttrModel = new GoodsAttr;
			//属性添加
			foreach (Q('post.attr') as $taid => $v) {
				if($v){
					$data = array(
						'gtvalue'=>$v,
						'taid'=>$taid,
						'gid'=>$gid
					);
					$goodsAttrModel->add($data);
				}
			}
						
			//规格添加
			$spec = Q('post.spec');
			if($spec){
				$value = $spec['value'];
				foreach ($value as $taid => $v) {
					foreach ($v as $kk => $vv) {
						$data = array(
							'gtvalue'=>$vv,
							'taid'=>$taid,
							'gid'=>$gid,
							'added'=>$spec['added'][$taid][$kk]
						);
						$goodsAttrModel->add($data);
					}
				}
			}
			
			return true;
		}
		
		return false;
	}
	
	
}

    
?>